package com.lyz.dz.des.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lyz.dz.des.pojo.status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.*;
import java.nio.charset.StandardCharsets;
@Controller
public class baseController {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    com.lyz.dz.des.pojo.message message;
    public String responseMessage(Object o) throws JsonProcessingException {
        return objectMapper.writeValueAsString(o);
    }
    public String success(Object o) throws JsonProcessingException {
        message.setMessage(status.Operation_success);
        message.setCode(status.ServerCode_success);
        message.setDate(o);
        return responseMessage(message);
    }
    public String error(String code, String errMessage ) throws JsonProcessingException {
        message.setCode(code.isEmpty()?status.ServerCode_error:code);
        message.setMessage(errMessage.isEmpty()?status.Server_error:errMessage);
        return  responseMessage(message);
    }
}
